package gui;
import java.awt.*;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import javax.swing.*;


import protocol.GameStateTypes;

import chat.CtfChat;
import client.Main;
import client.CtfState;

import response.MoveBody;
import structures.*;

import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;


/**
 * GUI: graphical representation of the CTF maze.
 * 
 * @author WHOMP
 */
public class CtfLogin extends JComponent {
	/**
	 * Default for Serializable class
	 */
	private static final long serialVersionUID = -5619959871432679035L;
	/*
	 * Static final variables
	 */

	static final BufferedImage TITLE_IMAGE = loadImage("title-screen");
	
	public static final int CELL_LENGTH = 50; // unit height and width
	
	// GUI Options
	static final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	public static final int WINDOW_WIDTH = (800/CELL_LENGTH) * CELL_LENGTH;	// screenSize.width
	public static final int WINDOW_HEIGHT = (600/CELL_LENGTH) * CELL_LENGTH;	// screenSize.height
	
	public static TextField ip;
	public static TextField user;
	
	/*
	 * Instance variables
	 */

	static JFrame frame;
	private static CtfLogin gui;
	Panel panel;

	
	/*
	 * Constructor
	 * 		Initializes JFrame window.
	 */
	public CtfLogin( String title, JFrame jf ) { 
	
		frame = jf;

		setFrameOptions();
		
		// show
		frame.setVisible(true);
		
		// assign to private representation
		gui = this;
		
		
		
		// login
		
		panel = new Panel();
		
//		login.setSize( 250, 150 );
//		login.setVisible(true);
//		frame.add(login);
		
		
		panel.setBackground(Color.white);
        panel.setLayout(new FlowLayout());

        panel.add(new Label("Server IP : "));

        ip = new TextField("localhost", 16);
//        {
//
//            /**
//			 * Make this text field immutable
//			 */
//			private static final long serialVersionUID = 1L;
//
//			@Override
//            public boolean keyDown(Event evt, int key) {
//                
//                return true;
//            }
//        };
        ip.addActionListener(new ButtonListener());
        panel.add(ip);

        panel.add(new Label("Username : "));

        user = new TextField("WHOMP", 16);
        user.addActionListener(new ButtonListener());
        
        panel.add(user);

        JButton login = new JButton("Login");
        login.addActionListener(new ButtonListener());
        panel.add(login);

        panel.setSize( 180, 150);
        panel.setLocation( (CtfGui.WINDOW_WIDTH / 2) - (180 / 2), 
        	(CtfGui.WINDOW_HEIGHT / 2) + 50 );

       
        this.add(panel);
        panel.setVisible(true);
		frame.repaint();
		panel.repaint();
		
		//panel.repaint();
		
	}
	
	public static CtfLogin getInstance() {
		// if not initialized, will be null
		return gui;
	}
	
	/**
	 * Set frame options.
	 */
	public void setFrameOptions() {
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		frame.getContentPane().add(this);
		
		Dimension d = new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT);
		this.setPreferredSize( d );
		
		// scrollbars as needed
		//JScrollPane scrollBars = new JScrollPane( this );
		//frame.add( scrollBars );
		
		// focus listener
		// Goes back to JavaClient.main(), kills component
//		CtfFocusListener fl = new CtfFocusListener();
//		frame.addFocusListener(fl);
		
		// key listener
		CtfKeyListener kl = new CtfKeyListener();
		frame.addKeyListener(kl);
		
		// frame size
		frame.setSize( WINDOW_WIDTH, WINDOW_HEIGHT );
	}
	
	
	public void closeGui() {
		frame.remove(this);
	}
	
	/**
	 * Paints 
	 */
	public void paint(Graphics g) {
		System.out.println( g.drawImage(TITLE_IMAGE, 0, 0, WINDOW_WIDTH-15, WINDOW_HEIGHT-38,  null));
		panel.repaint();
	}
	
	public static void paintImage(Graphics g, BufferedImage img, 
			int imgX, int imgY) {
    	g.drawImage(img, imgX, imgY, CELL_LENGTH, CELL_LENGTH,  null);
	}
	/**
	 * Load a specified image in src/gui/images/ directory
	 * @param filename
	 * @return
	 */
	public static BufferedImage loadImage(String filename) {
		BufferedImage img = null;
		try {
		    img = ImageIO.read( new FileImageInputStream(new File("src/gui/images/"+filename+".png")));
		    return img;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}


class ButtonListener implements ActionListener { //[C.U.004]

    ButtonListener() {
    }

    public void actionPerformed(ActionEvent e) {
        Main.isLoggingIn = false;
    }
}


